package ipad.asia.service.enterprise.impl;

import ipad.asia.dao.DiscountDao;
import ipad.asia.entity.Discount;
import ipad.asia.entity.Product;
import ipad.asia.service.DiscountService;
import ipad.asia.service.ProductService;
import ipad.asia.utils.MyTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
@Transactional
public class DiscountServiceImpl implements DiscountService {
    @Autowired
    private DiscountDao discountDao;
    @Autowired
    private ProductService productService;
    MyTime myTime=new MyTime();
    //实现折扣接口
    @Override
    public void SetDiscount(int productId, Float discount) {
       Product product= productService.findById(productId);
       productService.save(product.setHaveDiscount(1));
       discountDao.save(new Discount().setProductId(productId).setDiscount(discount).setDate(myTime.LocalDateTime()));
    }
    //实现降价接口
    @Override
    public void SetReduce(int productId, Float reduce) {
        Product product= productService.findById(productId);
        productService.save(product.setHaveDiscount(1));
        discountDao.save(new Discount().setProductId(productId).setReduce(reduce).setDate(myTime.LocalDateTime()));
    }
    //实现秒杀接口
    @Override
    public void SetSecKill(int productId, Float secKill) {
        Product product= productService.findById(productId);
        productService.save(product.setHaveDiscount(1));
        discountDao.save(new Discount().setProductId(productId).setSeckill(secKill).setDate(myTime.LocalDateTime()));
    }
    //实现优惠券接口
    @Override
    public void SetCoupon(int productId, Float coupon) {
        Product product= productService.findById(productId);
        productService.save(product.setHaveDiscount(1));
        discountDao.save(new Discount().setProductId(productId).setCoupon(coupon).setDate(myTime.LocalDateTime()));
    }
    //实现删除优惠接口
    @Override
    public void delDiscount(int productId) {
        Product product= productService.findById(productId);
        productService.save(product.setHaveDiscount(0));
        discountDao.deleteByProductId(productId);
    }

    @Override
    public Discount findDiscount(int productId) {
        return discountDao.findByProductId(productId);
    }
}
